if ( CLIENT ) then return end // This has no use on the client.


CreateConVar( "shields_enabled", 1, FCVAR_ARCHIVE )
CreateConVar( "shieldsregen_speed", 3, FCVAR_ARCHIVE )
CreateConVar( "shields_defaultmax", 50, FCVAR_ARCHIVE )
CreateConVar( "shields_delay", 8, FCVAR_ARCHIVE )
CreateConVar( "shields_spawn", 0, FCVAR_ARCHIVE )


AddCSLuaFile("autorun/client/shieldhud.lua")


local function Think()
	local enabled = GetConVarNumber( "shields_enabled" ) > 0
	local speed = 1 / GetConVarNumber( "shieldsregen_speed" )
	local time = FrameTime()
	if ( !enabled ) then return end
	
	for _, ply in pairs( player.GetAll() ) do
		if ( ply:Alive() ) then
		
			if ( ( ply.shieldMax or 0 ) == 0 ) then
				ply.shieldMax = GetConVarNumber( "shields_defaultmax" )
			end
			local max = ply.shieldMax
			
			if ( ( ply.shieldEnabled or -1 ) == -1 ) then
				if ( GetConVarNumber( "shields_spawn" ) > 0 ) then
					ply.shieldEnabled = 1
					else
					ply.shieldEnabled = 0
					end
					
			end
			if ( ply.shieldEnabled > 0 ) then 
				ply.shieldAmount = ( ply.shieldAmount or 0 )
				shield = ply.shieldAmount
				
				
				if ( shield < ( ply.LastShield or 0 ) || ply:Health() < ( ply.LastHealth or 0 ) ) then
					ply.ShieldRegenNext = CurTime() + GetConVarNumber( "shields_delay" )
				end
				
				if ( CurTime() > ( ply.ShieldRegenNext or 0 ) && enabled ) then
					ply.ShieldRegen = ( ply.ShieldRegen or 0 ) + time
					if ( ply.ShieldRegen >= speed ) then
						local add = math.floor( ply.ShieldRegen / speed )
						ply.ShieldRegen = ply.ShieldRegen - ( add * speed )
						if ( shield < max || speed < 0 ) then
							if ( shield <= 0 ) then
							ply:EmitSound( "weapons/physcannon/physcannon_charge.wav", 100, 100 )
							end
							if ( shield + add >= max ) then
							ply:EmitSound( "weapons/physcannon/physcannon_claws_close.wav", 100, 100 )
							end
							ply.shieldAmount = math.min( shield + add, max )
						end
					end
				end
				if ( ply.LastShield != ply.shieldAmount ) then
					local filter = RecipientFilter()
					filter:AddPlayer( ply )
					umsg.Start( "ShieldSys", filter )
					umsg.Float( ply.shieldAmount )
					umsg.Long( max )
					umsg.End()
				end
				ply.LastShield = ply.shieldAmount
				ply.LastHealth = ply:Health()
			end
		else
			ply.shieldEnabled = GetConVarNumber( "shields_spawn" )
			ply.shieldAmount = 0
			ply.shieldMax = GetConVarNumber( "shields_defaultmax" )
		end
	end
end

function EntityTakeDamage( ent, dmginfo )
	if ( GetConVarNumber( "shields_enabled" ) <= 0 ) then return end
	if ( ( ent.shieldEnabled or 0 ) == 0 ) then return end
	if ( ent:IsPlayer() ) then
		if ( ent.shieldAmount > 0 ) then
			if ( dmginfo:IsDamageType ( DMG_RADIATION ) ) then
				dmginfo:SetDamage(0)
				return end
			if ( dmginfo:IsDamageType ( DMG_FALL ) ) then return end
			//dmginfo:SetDamageType( DMG_CLUB )
			ent.shieldAmount = ent.shieldAmount - dmginfo:GetDamage()
			dmginfo:SetDamage(0)
			local point = ent:GetPos() + Vector( 0, 0, 10 )
			local effectdata = EffectData()
			effectdata:SetStart( point )
			effectdata:SetOrigin( point )
			effectdata:SetEntity( ent )

			if ( ent.shieldAmount <= 0 ) then
				ent:EmitSound( "physics/glass/glass_sheet_break3.wav", 50, 100 )
				ent:EmitSound( "ambient/energy/powerdown2.wav", 50, 100 )
				ent.shieldAmount = 0
				util.Effect( "shieldplybreak", effectdata )
				//util.Effect( "shieldplybreak2", effectdata )
				else
					if ( dmginfo:GetAttacker() != nil ) then
			
					local effect = EffectData();
					effect:SetOrigin( dmginfo:GetDamagePosition() );
					local temppos = ent:GetPos()
					local temppos2 = dmginfo:GetAttacker():GetPos()
					temppos = temppos2 - temppos;
					effect:SetNormal( temppos:GetNormal() );
					effect:SetEntity( ent );

					util.Effect( "ShieldImpact", effect );
					end
				//util.Effect( "shieldplyeffect", effectdata )
				//util.Effect( "shieldplyeffect2", effectdata )
				ent:EmitSound( table.Random{"physics/glass/glass_sheet_impact_hard1.wav",
											"physics/glass/glass_sheet_impact_hard2.wav",
											"physics/glass/glass_sheet_impact_hard3.wav"}, 50, 100)
				end
		else
			end
		end
	end

hook.Add( "EntityTakeDamage", "ShieldSys.Abs", EntityTakeDamage )
hook.Add( "Think", "ShieldSys.Think", Think )